//package com.ksd.system.service.impl;
//
//import com.ksd.common.config.PlayControlConfig;
//import com.ksd.common.core.domain.AjaxResult;
//import com.ksd.common.enums.UserStatus;
//import com.ksd.common.utils.StringUtils;
//import com.ksd.common.utils.file.FileUploadUtils;
//import com.ksd.system.domain.SysContentData;
//import com.ksd.system.domain.SysTvData;
//import com.ksd.system.domain.SysTvType;
//import com.ksd.system.service.*;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import javax.xml.bind.JAXBContext;
//import javax.xml.bind.JAXBException;
//import javax.xml.bind.Unmarshaller;
//import java.io.File;
//import java.util.ArrayList;
//import java.util.List;
//
//@Service
//public class MovieService {
//
//    public final Logger logger = LoggerFactory.getLogger(MovieService.class);
//    @Autowired
//    private ISysOperLogService sysOperLogService;
//    @Autowired
//    private ISysContentDataService sysContentDataService;
//    @Autowired
//    private ISysTvDataService sysTvDataService;
//    @Autowired
//    private ISysTvTypeService sysTvTypeService;
//    @Autowired
//    private ISysBatchService sysBatchService;
//
//    public static final String[] IMAGE_EXTENSION = { "bmp", "gif", "jpg", "jpeg", "png" ,"webp"};
//
//    public static final String[] FILE_EXTENSION = { "swf", "flv","mp4","3gp","mts","ts","mov","mkv","avi" };
//
//    public static final String[] XML_NAME = { "config.xml", "config.xml.done" };
//
//    public AjaxResult setMovieUpdateFile(String uploadFilePath, String userName, String taskId) {
//        System.out.println("开始进行电影的config.xml文件的解析");
//        System.out.println("选中已上传的文件路径,需要是导入电影文件的三层文件夹:"+uploadFilePath);
//        File file = new File(uploadFilePath);
//
//        /**
//         * 解压缩文件格式:视频格式:.mp4/
//         * 图片:.png/.jpg
//         */
//        List<File> xmlFiles = new ArrayList<>();
//        List<File> videoFiles = new ArrayList<>();
//        List<File> imageFiles = new ArrayList<>();
//        List<File> recommendBannerFiles = new ArrayList<>();
//        String filePathStart = PlayControlConfig.getZipFilePath()+File.separator+file.getName();
//        System.out.println("实际文件存储地址:"+filePathStart);
//        //进行文件区分
//        if (file.isDirectory()) {
//            File[] files = file.listFiles();
//            System.out.println("files size:"+files.length);
//            for (File everyFile : files) {
//                String name = everyFile.getName();
//                int i = name.lastIndexOf(".");
//                String substring = name.substring(i+1, name.length());
////                if (name.endsWith(".xml")) {
//                if (FileUploadUtils.isAllowedExtension(name, XML_NAME)){
//                    xmlFiles.add(everyFile);
//                }
//                if(FileUploadUtils.isAllowedExtension(substring,FILE_EXTENSION)){
//                    videoFiles.add(everyFile);
//                }
//                if(FileUploadUtils.isAllowedExtension(substring,IMAGE_EXTENSION)){
//                    if (name.startsWith("logo")){
//                        imageFiles.add(everyFile);
//                    }
//                    if (name.startsWith("recommend")){
//                        recommendBannerFiles.add(everyFile);
//                    }
//                }
//            }
//        }
//        if (StringUtils.isNotNull(xmlFiles) && StringUtils.isNotNull(videoFiles) && StringUtils.isNotNull(imageFiles) ) {
//            System.out.println("文件格式正确");
//        }else{
//            System.out.println("文件格式不正确");
//            // sysBatchService.deleteSysBatchByTaskId(taskId);
//            return AjaxResult.error("压缩包内的文件格式不正确,请校验文件");
//        }
//        SysContentData sysContentData = new SysContentData();
//
//        File xmlFile = xmlFiles.get(0);
//        String absolutePath = xmlFile.getAbsolutePath();
//        try {
//            JAXBContext jaxbContext = JAXBContext.newInstance(MovieResources.class);
//            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
//            MovieResources resources = (MovieResources) unmarshaller.unmarshal(new File(absolutePath));
//            MovieContent content = resources.getContent();
//            String subcategory = content.getSubcategory();
//            if (subcategory.indexOf("/") != -1) {
//                String[] split = subcategory.split("/");
//                String type = split[0];
//                String typeData = split[1];
//
//                SysTvType sysTvType = new SysTvType();
//                sysTvType.setName(type);
//                List<SysTvType> sysTvTypes = sysTvTypeService.selectSysTvTypeList(sysTvType);
//                Long sysTvTypeId;
//                if (sysTvTypes.size() > 0) {
//                    sysTvTypeId = sysTvTypes.get(0).getId();
//                }else{
//                    sysTvType.setStatus(UserStatus.OK.getCode());
//                    sysTvTypeService.insertSysTvType(sysTvType);
//                    sysTvTypeId = sysTvType.getId();
//                }
//                sysContentData.setFileType(Integer.valueOf(sysTvTypeId.toString()));
//
//                SysTvData sysTvData = new SysTvData();
//                sysTvData.setTypeId(sysTvTypeId);
//                sysTvData.setName(typeData);
//                List<SysTvData> sysTvDatas = sysTvDataService.selectSysTvDataList(sysTvData);
//                Long tvDataId;
//                if (sysTvDatas.size() > 0) {
//                    tvDataId = sysTvDatas.get(0).getId();
//                }else{
//                    sysTvData.setStatus(UserStatus.OK.getCode());
//                    sysTvDataService.insertSysTvData(sysTvData);
//                    tvDataId = sysTvData.getId();
//                }
//                sysContentData.setTypeData(Integer.valueOf(tvDataId.toString()));
//            }
//
//            String videoFileSql = PlayControlConfig.getMappedAddress()+File.separator+file.getName()+content.getVideoFile();
//            System.out.println("校验数据库文件存储地址:"+videoFileSql);
//            sysContentData.setTitle(StringUtils.trim(content.getTitle()));
//            sysContentData.setTaskId(taskId);
//            sysContentData.setUrlAddress(filePathStart+content.getVideoFile());
//            System.out.println("即将存入的数据库地址:"+sysContentData.getUrlAddress());
//            sysContentData.setImgAddress(filePathStart+content.getImageFile());
//            sysContentData.setDescription(content.getDescription());
//            Recommend recommend = content.getRecommend();
//            if (StringUtils.isNotNull(recommend)){
//                sysContentData.setIsRecommend(StringUtils.trim(recommend.getIsRecommend()));
//                if (sysContentData.getIsRecommend().equals("1")){
//                    sysContentData.setRecommendBanner(filePathStart+StringUtils.trim(recommend.getRecommendBanner()));
//                }
//            }
//            sysContentData.setStatus(VodStatus.OK.getCode());
//            sysContentData.setUserName(userName);
//
//            System.out.println("title:"+sysContentData.getTitle());
//            String keyWord = ChinesePinyinConverter.convertToPinyin(sysContentData.getTitle()).replaceAll("\\s+", "");
//            System.out.println("keyWord:"+keyWord);
//            sysContentData.setKeyWord(keyWord);
//
//
//
//            //插入之前需要查询一下该文件内容是否已上传,根据视频地址
//            SysContentData sysContentDataTemp = new SysContentData();
//            sysContentDataTemp.setUrlAddress(videoFileSql);
//            List<SysContentData> sysContentData1 = sysContentDataService.selectSysContentDataList(sysContentDataTemp);
//            System.out.println("查询数据上传文件是否已入库:"+sysContentData1.size()+"======"+videoFileSql);
//            if (sysContentData1.size() > 0) {
//                sysContentData.setId(sysContentData1.get(0).getId());
//                sysContentDataService.updateSysContentData(sysContentData);
//                sysContentData = sysContentDataService.selectSysContentDataById(sysContentData1.get(0).getId());
//            }else{
//                sysContentDataService.insertSysContentData(sysContentData);
//            }
//
//        } catch (JAXBException e) {
//            e.printStackTrace();
//        }
//        sysContentData = sysContentDataService.selectSysContentDataById(sysContentData.getId());
//        String imgAddress = sysContentData.getImgAddress();
//        String urlAddress = sysContentData.getUrlAddress();
//        String recommendBanner = sysContentData.getRecommendBanner();
//
//        File videoFile = videoFiles.get(0);
//        File imageFile = imageFiles.get(0);
//        if (StringUtils.isNotEmpty(recommendBannerFiles)){
//            File recommendBannerFile = recommendBannerFiles.get(0);
//            String recommendBannerFilePath = recommendBannerFile.getAbsolutePath();
//            FileMoveUsingIO.moveFile(recommendBannerFilePath,recommendBanner);
//        }
//        String xmlFilePath = xmlFile.getAbsolutePath();
//        String videoFilePath = videoFile.getAbsolutePath();
//        String imageFilePath = imageFile.getAbsolutePath();
//
//        System.out.println("移动文件地址videoFilePath:"+videoFilePath);
//        System.out.println("移动文件地址urlAddress:"+urlAddress);
//        FileMoveUsingIO.moveFile(videoFilePath,urlAddress);
//        //由于服务器存储空间不够,暂时不导入视频文件,只导入图片
//        //直接将原视频文件删除
//
//        FileMoveUsingIO.moveFile(imageFilePath,imgAddress);
//
//
//        String name = xmlFile.getName();
//        File fileTemp = new File(urlAddress);
//        String parent = fileTemp.getParent();
//        String newXmlPath = parent + File.separator + name;
//        FileMoveUsingIO.moveFile(xmlFilePath,newXmlPath);
//
//        //进行文件映射地址的更新
//        try {
//            JAXBContext jaxbContext = JAXBContext.newInstance(MovieResources.class);
//            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
//            MovieResources resources = (MovieResources) unmarshaller.unmarshal(new File(absolutePath));
//            MovieContent content = resources.getContent();
//
//            SysContentData sysContentDataTemp = sysContentDataService.selectSysContentDataById(sysContentData.getId());
//            String mapperTemp = PlayControlConfig.getMappedAddress()+File.separator+file.getName();
//            System.out.println("映射地址:"+mapperTemp);
//            sysContentDataTemp.setImgAddress(mapperTemp+content.getImageFile());
//            sysContentDataTemp.setUrlAddress(mapperTemp+content.getVideoFile());
//            sysContentDataTemp.setImgAddress(mapperTemp+content.getImageFile());
//            Recommend recommend = content.getRecommend();
//            sysContentDataTemp.setRecommendBanner(mapperTemp+StringUtils.trim(recommend.getRecommendBanner()));
//            sysContentDataService.updateSysContentData(sysContentDataTemp);
//        } catch (JAXBException e) {
//            e.printStackTrace();
//        }
//
//        return AjaxResult.success();
//    }
//}
